package GUI;

import ControlLayer.*;
import ModelLayer.*;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableColumn;
import javax.swing.UIManager;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

public class PersonManagement implements ItemListener {

	private JFrame frame;
	private JPanel insert;
	private JPanel panelEmploy;
	private JPanel panelCust;
	private JTextField textField_compName;
	private JTextField textField_CVR;
	private JTextField textField_Name;
	private JTextField textField_Address;
	private JTextField textField_Zipcode;
	private JTextField textField_phone;
	private JTextField textField_email;
	private JButton updateButton;
	private JLabel phoneLength;
	private PersonCtr personCtr;
	private JComboBox comboBox_cust;
	private JComboBox comboType;
	private JComboBox comboBox_Jobtype;
	private JCheckBox updatecheck;
	private JTextField textField_City;
	private PersonTable personTable;
	private JScrollPane scrollPane;
	private JTable table_persons;
	private JButton btnFindPerson;
	private JTextField textField_findPerson;
	private JLabel lblDelete;
	private JLabel lblFindPerson;
	private JLabel label_delete;
	private JTextField textField_deletePerson;
	private JButton btnNewButton;
	private JCheckBox chckDelete;

	/**
	 * Create the application.
	 */
	public PersonManagement() {

		makeFrame();
		frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void makeFrame() {
		frame = new JFrame();
		frame.setBounds(100, 100, 887, 491);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		makePanels();
	}

	// Creates panels
	private void makePanels() {

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 11, 851, 431);
		frame.getContentPane().add(tabbedPane);

		insert = new JPanel();
		tabbedPane.addTab("Insert / Update Person", null, insert, null);
		insert.setLayout(null);

		panelEmploy = new JPanel();
		panelEmploy.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Special for an employee",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelEmploy.setBounds(570, 16, 266, 59);
		insert.add(panelEmploy);
		panelEmploy.setLayout(null);

		panelCust = new JPanel();
		panelCust.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Special for a customer",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelCust.setBounds(570, 84, 266, 101);
		insert.add(panelCust);
		panelCust.setLayout(null);

		JPanel find = new JPanel();
		tabbedPane.addTab("Find Person", null, find, null);
		find.setLayout(null);

		scrollPane = new JScrollPane();
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(10, 92, 752, 226);
		find.add(scrollPane);

		table_persons = new JTable();
		scrollPane.setViewportView(table_persons);
		table_persons.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		personTable = new PersonTable();
		table_persons.setModel(personTable);

		JButton btnListall = new JButton("ListAll");
		btnListall.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				listAllPersons();
			}
		});
		btnListall.setBounds(540, 58, 89, 23);
		find.add(btnListall);

		JButton btnListEmployees = new JButton("List employees");
		btnListEmployees.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listAllEmployees();
			}
		});
		btnListEmployees.setBounds(401, 58, 129, 23);
		find.add(btnListEmployees);

		JButton btnListCustomers = new JButton("List customers");
		btnListCustomers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				listCustomers();
			}
		});
		btnListCustomers.setBounds(267, 58, 125, 23);
		find.add(btnListCustomers);

		btnFindPerson = new JButton("Find Person");
		btnFindPerson.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				textField_findPerson.setText("");
			}
		});
		btnFindPerson.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				findPerson();
			}
		});
		btnFindPerson.setBounds(135, 58, 121, 23);
		find.add(btnFindPerson);

		textField_findPerson = new JTextField();
		textField_findPerson.setBounds(10, 61, 115, 20);
		find.add(textField_findPerson);
		textField_findPerson.setColumns(10);

		JLabel lblInsertPhonenumber = new JLabel("Insert phonenumber");
		lblInsertPhonenumber.setBounds(10, 45, 147, 14);
		find.add(lblInsertPhonenumber);

		lblDelete = new JLabel("Delete person");
		lblDelete.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDelete.setBounds(10, 329, 115, 23);
		find.add(lblDelete);

		lblFindPerson = new JLabel("Find person");
		lblFindPerson.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblFindPerson.setVerticalAlignment(SwingConstants.TOP);
		lblFindPerson.setBounds(10, 11, 129, 23);
		find.add(lblFindPerson);

		label_delete = new JLabel("Insert phonenumber");
		label_delete.setBounds(145, 363, 147, 14);
		label_delete.setVisible(false);
		find.add(label_delete);

		textField_deletePerson = new JTextField();
		textField_deletePerson.setColumns(10);
		textField_deletePerson.setBounds(277, 360, 115, 20);
		textField_deletePerson.setVisible(false);
		find.add(textField_deletePerson);

		btnNewButton = new JButton("Delete selected");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				deletePerson();
			}
		});
		btnNewButton.setBounds(10, 363, 115, 23);
		find.add(btnNewButton);

		chckDelete = new JCheckBox("Delete from phonenumber");
		chckDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				deleteCheck();
			}
		});
		chckDelete.setBounds(135, 331, 173, 23);
		find.add(chckDelete);

		makeLabels();
		makeButtons();
		makeTextFields();
	}

	// Creates labels
	private void makeLabels() {
		JLabel label = new JLabel("Insert Person Values");
		label.setBounds(10, 11, 151, 20);
		label.setFont(new Font("Tahoma", Font.PLAIN, 16));
		insert.add(label);

		JLabel label_1 = new JLabel("Name");
		label_1.setBounds(10, 49, 137, 14);
		insert.add(label_1);

		JLabel label_2 = new JLabel("Address");
		label_2.setBounds(10, 84, 137, 14);
		insert.add(label_2);

		JLabel label_3 = new JLabel("Zipcode");
		label_3.setBounds(10, 123, 137, 14);
		insert.add(label_3);

		JLabel label_4 = new JLabel("City");
		label_4.setBounds(10, 158, 137, 14);
		insert.add(label_4);

		JLabel label_5 = new JLabel("Phone number");
		label_5.setBounds(10, 196, 137, 14);
		insert.add(label_5);

		JLabel label_6 = new JLabel("Email");
		label_6.setBounds(10, 231, 137, 14);
		insert.add(label_6);

		JLabel label_8 = new JLabel("Job type");
		label_8.setBounds(10, 22, 127, 14);
		panelEmploy.add(label_8);

		comboBox_Jobtype = new JComboBox();
		comboBox_Jobtype.setModel(new DefaultComboBoxModel(new String[] {
				"Stock", "Sale", "chauffeur", "Young worker", "Other" }));
		comboBox_Jobtype.setBounds(147, 19, 109, 20);
		panelEmploy.add(comboBox_Jobtype);

		JLabel label_9 = new JLabel("Customer type");
		label_9.setBounds(10, 28, 140, 14);
		panelCust.add(label_9);

		JLabel label_10 = new JLabel("Company name");
		label_10.setBounds(10, 53, 140, 14);
		panelCust.add(label_10);

		JLabel label_11 = new JLabel("CVR Number");
		label_11.setBounds(10, 78, 140, 14);
		panelCust.add(label_11);

		phoneLength = new JLabel("Phonenumber must contain 8 digits");
		phoneLength.setForeground(new Color(255, 0, 0));
		phoneLength.setBackground(Color.WHITE);
		phoneLength.setFont(new Font("Tahoma", Font.ITALIC, 11));
		phoneLength.setBounds(276, 196, 177, 14);
		phoneLength.setVisible(false);
		insert.add(phoneLength);

	}

	// Creates buttons
	private void makeButtons() {
		JButton btnInsert = new JButton("Confirm insert");
		btnInsert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addPerson();
			}
		});
		btnInsert.setBounds(713, 369, 123, 23);
		insert.add(btnInsert);

		updateButton = new JButton("Confirm update");
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updatePerson();
			}
		});
		updateButton.setBounds(570, 369, 123, 23);
		updateButton.setVisible(false);
		insert.add(updateButton);

		JButton btnReturn = new JButton("Return");
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				closeMethod();
			}
		});
		btnReturn.setBounds(10, 369, 89, 23);
		insert.add(btnReturn);

		comboBox_cust = new JComboBox();
		comboBox_cust.setVisible(false);
		comboBox_cust.setModel(new DefaultComboBoxModel(new String[] {
				"Business", "Private" }));
		comboBox_cust.setBounds(147, 25, 109, 20);
		panelCust.add(comboBox_cust);

	}

	// Creates textfields
	private void makeTextFields() {

		textField_compName = new JTextField();
		textField_compName.setVisible(false);
		textField_compName.setColumns(10);
		textField_compName.setBounds(147, 50, 109, 20);
		panelCust.add(textField_compName);

		textField_CVR = new JTextField();
		textField_CVR.setVisible(false);
		textField_CVR.setColumns(10);
		textField_CVR.setBounds(147, 75, 109, 20);
		panelCust.add(textField_CVR);

		textField_Name = new JTextField();
		textField_Name.setColumns(10);
		textField_Name.setBounds(157, 46, 109, 20);
		insert.add(textField_Name);

		textField_Address = new JTextField();
		textField_Address.setColumns(10);
		textField_Address.setBounds(157, 81, 109, 20);
		insert.add(textField_Address);

		textField_City = new JTextField();
		textField_City.setEditable(false);
		textField_City.setColumns(10);
		textField_City.setBounds(157, 155, 109, 20);
		insert.add(textField_City);

		textField_Zipcode = new JTextField();
		textField_Zipcode.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				personCtr = new PersonCtr();
				String city = personCtr.getCityOnZipCode(Integer
						.parseInt(textField_Zipcode.getText()));

				if (city != null) {
					textField_City.setText(city);
				} else {
					textField_City.setText("City not found");
				}

			}
		});
		textField_Zipcode.setColumns(10);
		textField_Zipcode.setBounds(157, 120, 109, 20);
		insert.add(textField_Zipcode);

		textField_phone = new JTextField();
		textField_phone.setColumns(10);
		textField_phone.setBounds(157, 193, 109, 20);
		insert.add(textField_phone);

		this.textField_phone.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {

				String phoneNr = textField_phone.getText();

				if (phoneNr.length() != 8) {
					phoneLength.setVisible(true);
				}

			}

			@Override
			public void focusGained(FocusEvent e) {
				phoneLength.setVisible(false);
			}
		});

		textField_email = new JTextField();
		textField_email.setColumns(10);
		textField_email.setBounds(157, 228, 109, 20);
		insert.add(textField_email);

		updatecheck = new JCheckBox("Update Existing Person");
		updatecheck.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				onCheckBox();
			}
		});
		updatecheck.setBounds(570, 327, 164, 23);
		insert.add(updatecheck);

		JLabel label_type = new JLabel("Type");
		label_type.setBounds(10, 267, 137, 14);
		insert.add(label_type);

		comboType = new JComboBox();
		comboType.setModel(new DefaultComboBoxModel(new String[] { "Employee",
				"Customer" }));
		comboType.setBounds(157, 259, 109, 20);
		comboType.addItemListener(this);
		insert.add(comboType);

	}

	// Closes the window
	private void closeMethod() {
		frame.dispose();
	}

	// Method used for adding a person in the DB
	private void addPerson() {

		personCtr = new PersonCtr();
		int phoneNumberLength = 0;
		int cVR = 0;

		String name = textField_Name.getText();
		String address = textField_Address.getText();
		int zipCode = Integer.parseInt(textField_Zipcode.getText());
		int phoneNumber = Integer.parseInt(textField_phone.getText());
		String email = textField_email.getText();

		String jobType = comboBox_Jobtype.getSelectedItem().toString();

		String customerType = comboBox_cust.getSelectedItem().toString();
		String companyName = textField_compName.getText();

		if (textField_CVR.getText().length() > 0)
			cVR = Integer.parseInt(textField_CVR.getText());
		else
			cVR = 0;

		phoneNumberLength = String.valueOf(phoneNumber).trim().length();

		if (phoneNumberLength == 8
				&& comboType.getSelectedItem().toString() == "Employee") {
			personCtr.insertEmployee(name, address, zipCode, phoneNumber,
					email, jobType);
		}

		else if (phoneNumberLength == 8
				&& comboType.getSelectedItem().toString() == "Customer") {
			personCtr.insertCustomer(name, address, zipCode, phoneNumber,
					email, customerType, companyName, cVR);
		}

		else {
			System.out.print("Field not filled correctly");
		}

	}

	// Method used for updating a person in the DB
	private void updatePerson() {
		personCtr = new PersonCtr();

		int phoneNumberLength = 0;
		int cVR = 0;

		String name = textField_Name.getText();
		String address = textField_Address.getText();
		int zipCode = Integer.parseInt(textField_Zipcode.getText());
		int phoneNumber = Integer.parseInt(textField_phone.getText());
		String email = textField_email.getText();

		String jobType = comboBox_Jobtype.getSelectedItem().toString();

		String customerType = comboBox_cust.getSelectedItem().toString();
		String companyName = textField_compName.getText();

		if (textField_CVR.getText().length() > 0)
			cVR = Integer.parseInt(textField_CVR.getText());
		else
			cVR = 0;

		phoneNumberLength = String.valueOf(phoneNumber).trim().length();

		if (phoneNumberLength == 8
				&& comboType.getSelectedItem().toString() == "Employee") {
			personCtr.updateEmployee(name, address, zipCode, phoneNumber,
					email, jobType);
		} else if (phoneNumberLength == 8
				&& comboType.getSelectedItem().toString() == "Customer") {
			personCtr.updateCustomer(name, address, zipCode, phoneNumber,
					email, customerType, companyName, cVR);
		}

		else {
			System.out.print("Field not filled correctly");
		}

	}

	// Method used when using the update checkbox
	private void onCheckBox() {
		if (updatecheck.isSelected() == true) {

			updateButton.setVisible(true);
		}

		else {
			updateButton.setVisible(false);

		}
	}

	// Used for the combo box
	public void itemStateChanged(ItemEvent e) {
		int selection;
		selection = comboType.getSelectedIndex();
		if (selection == 1) {
			comboBox_cust.setVisible(true);
			textField_compName.setVisible(true);
			textField_CVR.setVisible(true);

			comboBox_Jobtype.setVisible(false);
		}
		if (selection == 0) {
			comboBox_Jobtype.setVisible(true);

			comboBox_cust.setVisible(false);
			textField_compName.setVisible(false);
			textField_CVR.setVisible(false);
		}
	}

	// Method used when listing all persons
	private void listAllPersons() {
		personCtr = new PersonCtr();
		personTable.setPersons(personCtr.listAllPersons());
	}

	// Method used to list all customers
	private void listCustomers() {
		personCtr = new PersonCtr();
		personTable.setCustomers(personCtr.listAllCustomers());
	}

	// Method used for listing all employees
	private void listAllEmployees() {
		personCtr = new PersonCtr();
		personTable.setEmployees(personCtr.listAllEmployees());
	}

	// Method used for finding a person in the DB
	private void findPerson() {
		personCtr = new PersonCtr();
		Person person = new Person();

		if (textField_findPerson.getText().length() != 8) {
			JOptionPane.showMessageDialog(null, "Phonenumber must be 8 digits");
		}

		else
			person = personCtr.findCustomer(Integer
					.parseInt(textField_findPerson.getText()));
					personTable.setPerson(person);

		if (person == null) {
			person = personCtr.findEmployee(Integer
					.parseInt(textField_findPerson.getText()));
				personTable.setPerson(person);
		}

		if (person == null) {
			JOptionPane.showMessageDialog(null, "No person found");
		}

	}

	// Method used for deleting a person
	private void deletePerson() {

		PersonCtr personCtr = new PersonCtr();
		int phoneNumber = 0;
		if (chckDelete.isSelected() == false) {
			Person person = personTable.getPerson(table_persons
					.getSelectedRow());
			personCtr.deletePerson(person.getPhoneNumber());
			textField_deletePerson.setText(null);
		} 
		else
		{
			phoneNumber = Integer.parseInt(textField_deletePerson.getText());
		personCtr.deletePerson(phoneNumber);
		textField_deletePerson.setText(null);
		}

	}
// Method making a check whether the chckDelete checkbox is selected or not
	private void deleteCheck() {
		if (chckDelete.isSelected() == true) {
			textField_deletePerson.setVisible(true);
			label_delete.setVisible(true);
		} else
		{
			textField_deletePerson.setVisible(false);
		label_delete.setVisible(false);
		}
	}
}
